package com.example.pdfsearch.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.pdfsearch.R;
import com.example.pdfsearch.util.Constants;
import com.example.pdfsearch.util.PDFUtil;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.tencent.smtt.sdk.TbsReaderView;

import java.io.File;

import io.reactivex.functions.Consumer;

import static com.example.pdfsearch.util.PDFUtil.toUtf8String;

/**
 * @作者: njb
 * @时间: 2019/9/11 20:31
 * @描述: pdf文件预览
 */
public class PDFPreViewActivity extends AppCompatActivity implements TbsReaderView.ReaderCallback {
    private TbsReaderView mTbsReaderView;
    private String mFileName;
    private TextView tv_download;
    private long mRequestId;
    private DownloadObserver mDownloadObserver;
    private ProgressBar progressBar_download;
    private DownloadManager mDownloadManager;
    private String fileName;
    private String fileUrl;
    private String TAG = PDFPreViewActivity.class.getSimpleName();
    private RelativeLayout rootRl;
    private TextView tv_title;
    private ImageView iv_back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf_preview);
        initIntent();
        initTbsReaderView();
        mTbsReaderView = new TbsReaderView(this, this);
        rootRl.addView(mTbsReaderView, new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        if ((fileUrl == null) || (fileUrl.length() <= 0)) {
            Toast.makeText(PDFPreViewActivity.this, "获取文件url出错了", Toast.LENGTH_SHORT).show();
            return;
        }
        mFileName = PDFUtil.parseName(fileUrl);
        requestPermission();

        isDown();
    }

    private void initIntent() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            fileUrl = getIntent().getExtras().getString("fileUrl");
            fileName = getIntent().getExtras().getString("fileName");
        }
    }

    //初始化TbsReaderView 5-3
    private void initTbsReaderView() {
        rootRl = findViewById(R.id.rl_tbsView);
        tv_download = findViewById(R.id.tv_download);
        progressBar_download = findViewById(R.id.progressBar_download);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText("PDF文件预览");
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void requestPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            rxPermissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(new Consumer<Permission>() {
                        @Override
                        public void accept(Permission permission) {
                            if (permission.granted) {
                                // 用户已经同意该权限


                                Log.d(TAG, permission.name + " is granted.");
                            } else if (permission.shouldShowRequestPermissionRationale) {
                                // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                                Log.d(TAG, permission.name + " is denied. More info should be provided.");
                            } else {
                                // 用户拒绝了该权限，并且选中『不再询问』
                                Log.d(TAG, permission.name + " is denied.");
                            }

                        }
                    });
        }
    }

    private void isDown() {
        if (isLocalExist() || fileUrl.contains("storage")) {
            tv_download.setText("打开文件");
            tv_download.setVisibility(View.GONE);
            displayFile();
        } else {
            if (!fileUrl.contains("http")) {
                new AlertDialog.Builder(PDFPreViewActivity.this)
                        .setTitle("温馨提示:")
                        .setMessage("文件的url地址不合法哟，无法进行下载")
                        .setCancelable(false)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                        }).create().show();
            }
            startDownload();
        }
    }


    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    private void queryDownloadStatus() {
        DownloadManager.Query query = new DownloadManager.Query()
                .setFilterById(mRequestId);
        Cursor cursor = null;
        try {
            cursor = mDownloadManager.query(query);
            if (cursor != null && cursor.moveToFirst()) {
                // 已经下载的字节数
                long currentBytes = cursor
                        .getLong(cursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                // 总需下载的字节数
                long totalBytes = cursor
                        .getLong(cursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                // 状态所在的列索引
                int status = cursor.getInt(cursor
                        .getColumnIndex(DownloadManager.COLUMN_STATUS));
                tv_download.setText("下载中...(" + PDFUtil.FormetFileSize(currentBytes)
                        + "/" + PDFUtil.FormetFileSize(totalBytes) + ")");
                // 将当前下载的字节数转化为进度位置
                int progress = (int) ((currentBytes * 1.0) / totalBytes * 100);
                progressBar_download.setProgress(progress);

                Log.i("downloadUpdate: ", currentBytes + " " + totalBytes + " "
                        + status + " " + progress);
                if (DownloadManager.STATUS_SUCCESSFUL == status
                        && tv_download.getVisibility() == View.VISIBLE) {
                    tv_download.setVisibility(View.GONE);
                    tv_download.performClick();
                    if (isLocalExist()) {
                        tv_download.setVisibility(View.GONE);
                        progressBar_download.setVisibility(View.GONE);
                        displayFile();
                    }
                }
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    /**
     * 加载显示文件内容
     */
    private void displayFile() {
        Bundle bundle = new Bundle();
        if (isLocalExist()) {
            bundle.putString("filePath", getLocalFile().getPath());
        }
        if (fileUrl.contains("storage")) {
            bundle.putString("filePath", fileUrl);
        }

        bundle.putString("tempPath", Environment.getExternalStorageDirectory().getPath());
        boolean result = mTbsReaderView.preOpen(PDFUtil.getFileType(mFileName), false);
        if (result) {
            mTbsReaderView.openFile(bundle);
        } else {

            File file = new File(getLocalFile().getPath());
            if (file.exists()) {
                Intent openintent = new Intent();
                openintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                String type = PDFUtil.getMIMEType(file);
                // 设置intent的data和Type属性。
                openintent.setDataAndType(Uri.fromFile(file), type);
                // 跳转
                startActivity(openintent);
                finish();
            }
        }
    }

    /**
     * 下载文件
     */
    @SuppressLint("NewApi")
    private void startDownload() {
        mDownloadObserver = new DownloadObserver(new Handler());
        getContentResolver().registerContentObserver(
                Uri.parse("content://downloads/my_downloads"), true,
                mDownloadObserver);

        mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        //将含有中文的url进行encode
        String mFileUrl = toUtf8String(fileUrl);
        try {
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(mFileUrl));
            request.setDestinationInExternalPublicDir(Constants.DOWNLOAD_PDF_PATH, mFileName);
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
            mRequestId = mDownloadManager.enqueue(request);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转页面
     *
     * @param context
     * @param fileUrl  文件url
     * @param fileName 文件名
     */
    public static void actionStart(Context context, String fileUrl, String fileName) {
        Intent intent = new Intent(context, PDFPreViewActivity.class);
        intent.putExtra("fileUrl", fileUrl);
        intent.putExtra("fileName", fileName);
        context.startActivity(intent);
    }


    /**
     * 是否为本地文件
     *
     * @return
     */
    private boolean isLocalExist() {
        return getLocalFile().exists();
    }

    /**
     * 本地文件目录
     *
     * @return
     */
    private File getLocalFile() {
        return new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), mFileName);
    }

    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {

    }


    private class DownloadObserver extends ContentObserver {

        private DownloadObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            queryDownloadStatus();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTbsReaderView.onStop();
        if (mDownloadObserver != null) {
            getContentResolver().unregisterContentObserver(mDownloadObserver);
        }
    }
}
